﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using OfficeOpenXml;
public class SaveExcel : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnGUI()
    {
        if( GUI.Button( new Rect( 200, 0, 100, 100 ), "Excel" ) )
        {
            WriteExcel( DataSync.Instance.GetDeserializedData() );
        }
    }
    public static void WriteExcel( System.Collections.Generic.Dictionary<System.Type, object> _dataAfterDeserialized )
    {
        foreach( var excel in _dataAfterDeserialized )
        {
            string excelName = excel.Key.ToString();

            Dictionary<Type, object> data = DataSync.Instance.GetDeserializedData();
            ProtobufGen.Item_ARRAY itemList = data[typeof( ProtobufGen.Item_ARRAY )] as ProtobufGen.Item_ARRAY;

            for( int i = 0; i < itemList.items.Count; i++ )
            {
                ProtobufGen.Item item = itemList.items[i];
                
            }

        }
    }
    /*public static void WriteExcel( string excelName, string sheetName, Dictionary<string, InitConfig.RankConfigDataStyle> RankDictionary = null )
    {
        string path = Application.dataPath + "/" + excelName + ".xls";
        FileInfo newFile = new FileInfo( path );
        if( newFile.Exists )
        {
            newFile.Delete();
            newFile = new FileInfo( path );
        }
        //
        using( ExcelPackage package = new ExcelPackage( newFile ) )
        {
            //在excel空文件添加新sheet
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add( sheetName );
            //添加列名
            worksheet.Cells[1, 1].Value = RankDictionary["id"].rank;
            worksheet.Cells[1, 2].Value = RankDictionary["id"].name;
            worksheet.Cells[1, 3].Value = RankDictionary["id"].grade;
            worksheet.Cells[1, 4].Value = RankDictionary["id"].headRes;

            for( int i = 1; i < RankDictionary.Count; i++ )
            {
                string id = i.ToString();
                string num = ( 1 + i ).ToString();
                worksheet.Cells["A" + num].Value = RankDictionary[id].rank;
                worksheet.Cells["B" + num].Value = RankDictionary[id].name;
                worksheet.Cells["C" + num].Value = RankDictionary[id].grade;
                worksheet.Cells["D" + num].Value = RankDictionary[id].headRes;

            }
            package.Save();
            Debug.Log( excelName + "保存成功！" );
        }
    }*/

}
